package jflowmap.model.graph;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import jflowmap.model.ontology.Context;
import jflowmap.model.ontology.Dimension;
import jflowmap.model.ontology.Value;

import com.google.common.collect.Lists;

public final class Edge {
	private final Node source;
	private final Node target;
	private final Graph graph;
	private final Map<Dimension, Value> values = new HashMap<Dimension, Value>();
		
	public static final List<Edge> sortByValues (Collection<Edge> edges, final Dimension inDimension) {
		final List<Edge> result = Lists.newArrayList(edges);
		Collections.sort(result, new Comparator<Edge>() {
			@Override
			public int compare(Edge arg0, Edge arg1) {
				double diff = arg0.getValue(inDimension).getValue() 
						- arg1.getValue(inDimension).getValue();
				if (diff > 0) return -1;
				if (diff < 0) return 1;
				return 0; 
			}
		});
		return result;
	}
	
	
	public Edge (final Node source, final Node target, final Graph graph) {
		this.source = source;
		this.target = target;
		this.graph = graph;
		
		source.addOutgoingEdge(this);
		target.addIncomingEdge(this);
	}
	
	public Node getOppositeNode (Node node) {
		if (node == source) return target;
		else return source;
	}
	
	public Node getSourceNode() {
		return source; 
	}
	
	public Node getTargetNode() {
		return target;
	}
	
	public Set<Dimension> getValueDimensions() {
		return Collections.unmodifiableSet(values.keySet());
	}
	
	public Set<Dimension> getNonMissingValueDimensions() {
		Set<Dimension> tmp = new HashSet<Dimension>();
		for (Dimension dimension: values.keySet()) {
			if(values.get(dimension) != Context.missingValue)
				tmp.add(dimension);
		}
		return tmp;
	}
	
	public Value getValue(Dimension dimension) {
		return values.get(dimension);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((source == null) ? 0 : source.hashCode());
		result = prime * result + ((target == null) ? 0 : target.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Edge other = (Edge) obj;
		if (source == null) {
			if (other.source != null)
				return false;
		} else if (!source.equals(other.source))
			return false;
		if (target == null) {
			if (other.target != null)
				return false;
		} else if (!target.equals(other.target))
			return false;
		return true;
	}

	public void addEdgeValue(Dimension valueDimension, Value value) {
		values.put(valueDimension, value);
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append(source + " -> " + target + "\n");
		for (Entry<Dimension, Value> entry: this.values.entrySet()) {
			builder.append(entry.getKey().getName() + ": " + entry.getValue().getValue());
			builder.append('\n');
		}
		return builder.toString();
	}

	public Graph getGraph() {
		return graph;
	}


	public Collection<Value> getValues() {
		return this.values.values();
	}
}
